using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Xml.Linq;


namespace Pokemon
{
    class Dragonite : Pokemon
    {
        private static Card _avatar;

        public static Card Avatar
        {
            get { return Dragonite._avatar; }
            set { Dragonite._avatar = value; }
        }

        public Dragonite(Game game, Blank owner, Vector2 position)
            : base(game, owner, position)
        {
        }

        public override void Initialize()
        {
            XDocument doc = XDocument.Load(@"Content/Textures/Pokemons/Dragonite/DragoniteDef.xml");
            Texture2D texture2 = null;
            Vector2 frameSize = Vector2.Zero;
            Vector2 centerPoint = Vector2.Zero;
            int numFrame;            

            if (doc.Document != null)
            {
                // get FrameWidth value
                XName name = XName.Get(@"FrameWidth");
                var definitions = doc.Document.Descendants(name);
                Int32 frameWidth = Int32.Parse((definitions.First()).Value);

                // get FrameHeight value
                name = XName.Get(@"FrameHeight");
                definitions = doc.Document.Descendants(name);
                Int32 frameHeight = Int32.Parse((definitions.First()).Value);

                // set size
                Size = new Vector2(frameWidth, frameHeight);

                // get Damage
                name = XName.Get(@"Damages");
                definitions = doc.Document.Descendants(name);
                // set Damage
                Damage = Int32.Parse((definitions.First()).Value);

                // get Health
                name = XName.Get(@"Healths");
                definitions = doc.Document.Descendants(name);
                _health = Int32.Parse((definitions.First()).Value);

                // get bull speed
                name = XName.Get(@"Speed");
                definitions = doc.Document.Descendants(name);
                _bulledSpeed = Int32.Parse((definitions.First()).Value);

                //get range
                name = XName.Get("Range");
                definitions = doc.Document.Descendants(name);
                _range = Int32.Parse((definitions.First()).Value);

                //get delay time(s)
                name = XName.Get("Delay");
                definitions = doc.Document.Descendants(name);
                _delay = Int32.Parse((definitions.First()).Value); 


                // get properties
                name = XName.Get(@"Definition");
                definitions = doc.Document.Descendants(name);

                foreach (var def in definitions)
                {
                    var xAttribute = def.Attribute(@"SheetName");
                    if (xAttribute != null)
                    {
                        texture2 = Game.Content.Load<Texture2D>(xAttribute.Value);
                        xAttribute = def.Attribute(@"FrameWidth");
                        if (xAttribute != null)
                        {
                            var width = Int32.Parse(xAttribute.Value);
                            xAttribute = def.Attribute(@"FrameHeight");
                            if (xAttribute != null)
                            {
                                var height = Int32.Parse(xAttribute.Value);
                                frameSize = new Vector2(width, height);    
                            }
                        }
                    }

                    xAttribute = def.Attribute(@"XCenterPoint");

                    var xCenterPoint = Int32.Parse(xAttribute.Value);
                    xAttribute = def.Attribute(@"YCenterPoint");
                    var yCenterPoint = Int32.Parse(xAttribute.Value);
                    centerPoint = new Vector2(xCenterPoint, yCenterPoint);

                    xAttribute = def.Attribute(@"NumFrame");
                    numFrame = Int32.Parse(xAttribute.Value);

                    xAttribute = def.Attribute(@"Name");
                    var type = xAttribute.Value;

                    Sprite sprite = new Sprite(texture2, numFrame, frameSize, centerPoint, false);

                    switch (type)
                    {
                        case "Idle":
                            Textures[0] = sprite;
                            break;
                        case "Attack":
                            Textures[1] = sprite;
                            break;
                        case "Hit":
                            Textures[2] = sprite;
                            break;
                        case "Dead":
                            Textures[3] = sprite;
                            break;
                        case "Stand":
                            Textures[4] = sprite;
                            break;
                    } 
                }

            }
            base.Initialize();
        }
        public override void Update(GameTime gameTime)
        {
            _delayTimeSpan += gameTime.ElapsedGameTime;
            _TimeSpan += gameTime.ElapsedGameTime;
            if (_TimeSpan.Milliseconds < 100)
                return;
            int index = GetUnitStateIndex();

            base.Update(gameTime);
            _TimeSpan = TimeSpan.Zero;

            // kiem tra xem tam ban cua Pokemon co phu muc tieu hay chua? va neu phu roi thi xem thoi gian delay giua 2 lan ban
            if (_enemy != null && Blank.GetDistance(GetCenterPoint(), _enemy.GetCenterPoint()) < _range && _delayTimeSpan.Seconds >= _delay)
            {
                // goi ham tan cong muc tieu
                Attack();
                // raise event ban ra dan
                Shot(_enemy);
                // gan thoi gian cho ban lai = 0
                _delayTimeSpan = TimeSpan.Zero;

            }
            if (UseSkill != null)
                OnUseSkill();

            Textures[index].Update();            
        }
        public override void Draw(GameTime gameTime)
        {
            Rectangle rect = GetNextRect();
            SpriteBatch _SpriteBatch = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));
            int index = GetUnitStateIndex();
            Textures[index].Draw(_SpriteBatch, _position, 
                rect, Color.White, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.FlipHorizontally, 0);       
            base.Draw(gameTime);
        }

        public override void Shot(Unit target)
        {
            AddEvent();
            //base.Shot(target);
        }        

        #region Event xu ly khi goi dan ban

        public delegate void UsingSkill();
        public event UsingSkill UseSkill;

        public void OnUseSkill()
        {
            UsingSkill hanlder = UseSkill;
            if (hanlder != null)
                hanlder();
        }


        public void DragoniteOnSkill()
        {
            int index = GetUnitStateIndex();
            // neu nhu khung hinh dich chuyen den trang thai ban ra dan
            if (Textures[index].FrameIndex < 3)  // && _UnitState == UnitState.Attack)
            {
                // tao ra dan 
                Beam b = new Beam(Game, this, _enemy);
                BulletManager t = (BulletManager)Game.Services.GetService(typeof(BulletManager));
                t.Add(b);
                // ban xong thi chuyen ve trang thai Idle
                if (Textures[index].FrameIndex >= 2)
                {
                    Idle();
                    RemoveEvent();
                }
            }
            
        }

        public void AddEvent()//gan event vao de doi tuong biet la no dang ra skill
        {
            UseSkill += DragoniteOnSkill;
        }

        public void RemoveEvent()
        {
            UseSkill -= DragoniteOnSkill;
        }
        #endregion
    }
}
